# 完成用户注册统计数量的
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.permissions import IsAdminUser
from datetime import date,timedelta

from goods.models import GoodsVisitCount
from users.models import User


class UserCountView(APIView):
    """
        用户总数统计
    """
    # 指定权限（只能是认证用户）
    permission_classes = [IsAdminUser]
    def get(self,request):
        # 1.查询所有注册用户，计算用户总量
        count=User.objects.filter(is_staff=False).count()
        # 2.返回用户数量
        return Response({
                'count':count,
            })
class UserDayCountView(APIView):
    """
        当天注册用户总数统计
    """
    permission_classes = [IsAdminUser]
    def get(self,request):
        # 1.获取当天日期
        now_date=date.today()
        # 2.根据当天日期查询当天的注册用户的总数
        count=User.objects.filter(date_joined__gte=now_date,is_staff=False).count()
        # 3.返回结果
        return Response({
            'count':count,
        })


class UserDayActiveCountView(APIView):
    """
        当天登陆用户总数统计
    """
    permission_classes = [IsAdminUser]
    def get(self,request):
        # 1.获取当天日期
        now_date=date.today()
        # 2.根据当天日期查询当天的当天用户的总数
        count=User.objects.filter(last_login__gte=now_date,is_staff=False).count()
        # 3.返回结果
        return Response({
            'count':count,
        })

class UserOrderCountView(APIView):
    """
        当天下订单用户总数统计
    """
    permission_classes = [IsAdminUser]
    def get(self,request):
        # 1.获取当天日期
        now_date=date.today()
        # 2.根据当天日期查询当天下订单的用户总数,用订单数据作为过滤条件查询用户信息
        users=set(User.objects.filter(orders__create_time__gte=now_date,is_staff=False))
        count=len(users)
        # 3.返回结果
        return Response({
            'count':count,
        })

class UserMonthCountView(APIView):
    """
        月增用户总数统计
    """
    permission_classes = [IsAdminUser]
    def get(self,request):
        # 1.获取当天日期
        now_date=date.today()
        # 2.根据当天的日期获取一个月之前的日期
        start_data=now_date-timedelta(30)
        # 3.遍历30天之内的每日注册数据
        data_list=[]
        for i in range(31):
            # 获取当天日期
            index_date=start_data+timedelta(i)
            # 获取下一天的日期
            next_date=start_data+timedelta(i+1)
            count=User.objects.filter(is_staff=False,date_joined__gte=index_date,
                                      date_joined__lt=next_date).count()
            data_list.append({
                'count':count,
                'date':index_date,
            })
        # 4.返回结果
        return Response(
            data_list
        )


class GoodsTypeCountView(APIView):
    """
        商品分类访问总数统计
    """
    permission_classes = [IsAdminUser]
    def get(self,request):
        # 1.获取当天日期
        now_date=date.today()
        # 2.根据当天日期查询商品分类访问表
        goods=GoodsVisitCount.objects.filter(date__gte=now_date)
        data_list=[]
        for good in goods:
            data_list.append({
                'count':good.count,
                'category':good.category.name,
            })
        # 3.返回结果
        return Response(data_list)